Pause
Show Transcript
Video info
00:00
Pause
Seek 10 seconds backwards
Seek 10 seconds forward
00:41 / 06:37
Mute
Click to volume controlUse the arrows to control the volume
Enable Captions
Settings
Picture in picture
Fullscreen
More transcript options
Hide Transcript
Skip transcript
00:02
As your programs start to
00:04
get larger and more complex,
00:06
it becomes important to organize
00:08
all of the pieces that go into them.
00:09
Also, you might find that you can
00:11
reuse some of these pieces.
00:13
Instead of copying and pasting code,
00:15
we organize these reusable pieces
00:17
into something we call functions.
00:19
A function is a bit of code
00:21
that you can reuse whenever you need it.
00:23
You've used functions written by
00:25
others that are built in with Python,
00:27
such as print and input.
00:29
In this video, you'll
00:31
learn how to write your own.
00:32
In order to use your own function
00:34
in Python, you need to define it.
00:36
Let's look at a simple function
00:37
and discuss what each of
00:39
the pieces are that
00:40
we use to define a function.
00:42
In this example, Df
00:45
is a keyword that stands for define.
00:47
It signals to Python
00:48
that we are defining a function.
00:50
Next, we give the function a name.
00:53
In this example,
00:54
welcome is the name of the function.
00:56
Right after the name, there
00:58
are a set of parentheses,
01:00
and that's followed by a colon.
01:03
The parentheses can contain what are
01:06
called parameters, or they can be empty.
01:09
This particular function doesn't need
01:11
any information from outside
01:13
the function in order to do its job,
01:15
so there are
01:16
no parameters in the parentheses.
01:18
The colon begins a code block.
01:21
Just like it does with the I
01:23
statement or for loop.
01:25
The string surrounded by
01:27
triple quotes is called
01:28
a documentation string or
01:29
doc string for short.
01:31
This is where we can describe
01:32
what the function does and include
01:34
any other useful information
01:36
for someone that might use our function.
01:38
It's not required, but it's helpful,
01:40
especially if our function is complicated.
01:43
In this example, we simply
01:44
state the purpose of the function,
01:46
which is to display a welcome message.
01:49
Next, we put the code we want to run
01:51
when our function is used or called.
01:54
Notice that everything after the colon,
01:58
including the dock string is indented.
02:00
Similar to other code blocks,
02:03
when we stop indenting,
02:04
the code block ends,
02:06
which will be the end of our function.
02:08
This function simply
02:09
prints a welcome message.
02:11
It's not that useful because it's so simple,
02:14
but we want to start out simple.
02:16
The last thing we see here is return.
02:20
This is a keyword and it is used to define
02:23
what will get sent back from our function.
02:26
In this case, there's nothing after return,
02:28
so it returns none.
02:30
Return isn't required in
02:32
our function if it doesn't return anything,
02:35
but it's here in
02:36
this example so we can mention it.
02:38
But I'll delete it because we don't need it.
02:40
Let's run our code and see what happens.
02:45
Well, our message didn't print out.
02:48
So why not?
02:51
Well, we've defined our function well,
02:54
but in order to run
02:56
the code inside the function,
02:57
we have to call the function.
03:00
Call it a function is easy.
03:02
You just use its name
03:03
followed by parentheses,
03:04
if our function had parameters,
03:06
we'd need to put
03:07
something in the parentheses,
03:09
but this function doesn't.
03:10
So let's call our function.
03:12
Since the name is welcome,
03:14
we'll just type welcome
03:16
with an empty set of parentheses.
03:19
Now let's run it and see if it works.
03:22
Wonderful. Welcome to the Function factory.
03:25
It worked. We've defined
03:27
and called our own function.
03:29
It's good practice to put
03:30
your function definitions at
03:32
the top of your code file.
03:33
Then when you need to use them,
03:35
just call them with their name.
03:36
The functions won't run
03:38
until they are called.
03:39
But Python will keep track of them
03:42
so they can be executed later in.
03:45
Now, let's do something different.
03:48
Let's change our function just a little
03:51
so that it is personalized
03:53
by using a parameter.
03:54
To personalize the message,
03:56
we need to know the name we
03:58
need to include in our message.
03:59
This means that our function
04:01
needs to know the name.
04:03
So we'll add a parameter
04:05
inside the parentheses.
04:06
The parameter will be
04:08
a variable that the function can use.
04:10
We'll use name in this case.
04:13
And then we need to change
04:15
our message so we can use
04:17
name in the message.
04:18
We'll say, Hello, name.
04:23
Welcome to the Function factory.
04:25
Now, let's run this and see how it works.
04:33
Uh oh, we got an error.
04:35
Well, that's because when
04:36
we called our function,
04:38
we didn't give it a parameter.
04:40
Notice it says missing
04:41
one required positional argument name.
04:44
So we need to get
04:46
that information from the user,
04:47
or we could hard code it.
04:49
Let's get it from the user.
04:51
Since we need the information
04:53
before we call the function,
04:55
we're going to need to get it from
04:56
the user before we call Welcome.
05:01
So we'll say user
05:05
equals input. What is your name?
05:10
Now, we're not quite done.
05:13
Notice that user is
05:14
a variable outside of the function.
05:16
When we call a function and put
05:18
a variable name in the parentheses,
05:19
that's called an argument,
05:21
and the value of the argument will be
05:22
passed to the parameter
05:23
in the function definition.
05:25
We still need to put that
05:26
argument in our welcome call.
05:29
This is how we send
05:30
information from outside of
05:32
the function into
05:33
the function so it can be used.
05:35
So here we're going to type
05:36
in user in the parentheses.
05:38
It's important to recognize
05:40
when you're defining a function that
05:42
you'll need parameters for
05:43
any information that is
05:45
needed inside the function,
05:47
but it is created or
05:48
defined outside of the function.
05:50
So when you define a function, ask yourself,
05:53
what information does this function
05:55
need in order to do its job?
05:56
And that will help you know
05:58
what parameters you need.
05:59
So let's run this code and see how it works.
06:03
It's going to ask for my name.
06:06
Jim Kristi. And while,
06:09
hello, Kristi,
06:10
welcome to the Function factory.
06:13
It worked great. Now, this
06:15
is a very simple example,
06:17
but functions are
06:18
powerful tools in programming.
06:20
And there are a lot more details
06:22
you'll learn to make them more powerful.
06:25
Think of some tasks you'd like Python to
06:28
perform and see if you can write
06:29
a function that performs that task.
Resume AutoScroll
Copy debug info